﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;

namespace Common
{
    public static class Helper
    {
        public static void SentMsg(string content)
        {
            try
            {
                string strURL = System.IO.Directory.GetCurrentDirectory()+"/处理日志.txt";
                System.IO.File.AppendAllText(strURL, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+":"+content+"\r\n");
                return;
                Encoding ed = Encoding.GetEncoding("gb2312");
                string phoneNumbers = System.Configuration.ConfigurationSettings.AppSettings["PhoneNumber"];
                string[] numberArr = phoneNumbers.Split(',');
                int index = 0;
                index = new Random().Next(0, numberArr.Length - 1);
                string userName = System.Configuration.ConfigurationSettings.AppSettings["SmsAccount"];
                string passWord = System.Configuration.ConfigurationSettings.AppSettings["SmsPassword"];
                string strRequest = "http://sms3new.01860.com.cn/?CORPID=IS001-0001&USERNAME=" + userName + "&PASSWORD=" + passWord + "&EXTNO=&MOBILE=" + numberArr[index] + "&CONTENT=" + HttpUtility.UrlEncode(content,ed) + "";
                WebClient wc = new WebClient();
                string str = wc.DownloadString(strRequest);
                if (str != "1")
                {
                    //发送失败
                }
            }
            catch { }
        }
        public static string CodeStr(string oldStr)
        { 
            Encoding ed = Encoding.GetEncoding("gb2312");
            return HttpUtility.UrlEncode(oldStr, ed);
        }
        public static CookieContainer cookies = new CookieContainer();
        public static string PostDataToServer(string postData, string url)
        {
            try
            {
                WebClient wc = new WebClient();
                //return wc.DownloadString(url+"?"+postData);
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] data = encoding.GetBytes(postData);
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
                myRequest.ContentLength = data.Length;
                myRequest.CookieContainer = cookies;
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                HttpWebResponse myReponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myReponse.GetResponseStream(), Encoding.Default);
                string content = reader.ReadToEnd();

                return content;
            }
            catch
            {
                return "";
            }
        }
    }
}
